'use strict'
//------------------------javascript实现责任链设计模式-----------------------

//------------------------Handler--------------------------------------------

function Checker(next) {
    this._next = next;
}

Checker.prototype = {
    /**
     * 传递责任链
     */
    handle: function(context){
        this._context = context;
        if( this.support() ){
            this.handleInternal();
        } else if(this._next) {
            this._next.handle( this._context );
        }
    },
    /**
     * 具体进行业务逻辑执行的代码,需要被覆盖
     */
    handleInternal: function () {
        
    },
    /**
     * 判断本类是否处理
     * 中了spring的毒,按他家的写把
     * @returns {boolean}
     */
    support: function () {
        return false;
    }
}

//------------------------ConcreteHandler---------------------------------------
var EmptyChecker,NumberChecker,RangeChecker;

RangeChecker = new Checker();
RangeChecker.handleInternal = function () {
    console.log('RangeChecker.handleInternal: '+this._context.value);
}
RangeChecker.support = function () {
    return this._context.value > 60;
}

NumberChecker = new Checker(RangeChecker);
NumberChecker.handleInternal = function () {
    console.log('NumberChecker.handleInternal: '+this._context.value);
}
NumberChecker.support = function () {
    return isNaN(this._context.value);
}

EmptyChecker = new Checker(NumberChecker);
EmptyChecker.handleInternal = function () {
    console.log('EmptyChecker.handleInternal: '+this._context.value);
}
EmptyChecker.support = function () {
    return !this._context.value || this._context.value=='';
}

//------------------------Client-----------------------------------------------

console.log(' 空 ');
EmptyChecker.handle({});
console.log(' 非数值 ');
EmptyChecker.handle({value: 'sd'});
console.log(' 数值34 ');
EmptyChecker.handle({value: 34});
console.log(' 数值符合要求134 ');
EmptyChecker.handle({value: 134});